package com.example.seakdreamandroid.accountInfo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.seakdreamandroid.MainActivity;
import com.example.seakdreamandroid.R;

public class IntroductionEditActivity extends AppCompatActivity {
    private TextView num;
    private ImageView imgReturn;
    private EditText editIntroduction;
    private Button save;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_introduction_edit);
        getView();
        initView();
        setListener();
    }

    private void setListener() {
        imgReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntroductionEditActivity.this.finish();
            }
        });
        editIntroduction.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                num.setText(120 - s.length() + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("introduction",editIntroduction.getText().toString());
                setResult(RESULT_OK,intent);
                IntroductionEditActivity.this.finish();
            }
        });
    }

    private void initView() {
        Intent intent = getIntent();
        String introductionStr = intent.getStringExtra("introduction");
        editIntroduction.setText(introductionStr);
        num.setText(120 - introductionStr.length() + "");
    }

    private void getView() {
        num = findViewById(R.id.num);
        imgReturn = findViewById(R.id.img_return);
        editIntroduction = findViewById(R.id.edit_introduction);
        save = findViewById(R.id.btn_save);
    }
}